import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/components/common/recommend.dart';
import 'package:cbec_app/components/order/after_sales_order_item.dart';
import 'package:cbec_app/components/order/order_item.dart';
import 'package:cbec_app/pages/my/controller/after_sales_orderlist_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

//售后列表
class AfterSalesOrderList extends GetView<AfterSalesOrderListController> {
  AfterSalesOrderList({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AfterSalesOrderListController>(builder: (controller) {
      return BasePage(
        title: "售后记录",
        refreshController: controller.baseRefreshCtr,
        enablePullDown: true,
        enablePullUp: controller.hasNextPage,
        onPageRefresh: controller.onPageRefresh,
        onPageLoading: controller.onPageLoading,
        iconType: IconType.back,
        tabHeight: 30.h,
        tabChildren: [_Tabs()],
        children: [
          Container(
              margin: EdgeInsets.only(top: 10.w),
              child: Container(
                color: Tools.fromHex("#F3F5F7"),
                padding: EdgeInsets.only(left: 8.w, right: 8.w, bottom: 20.w),
                child: controller.orderList.isNotEmpty
                    ? ListView.builder(
                        reverse: true,
                        controller: controller.scrollController,
                        physics: const NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: controller.orderList.length,
                        itemBuilder: (context, index) {
                          final List items = controller.orderList.reversed.toList();
                          Map<String, dynamic> item = items[index];
                          final List goodsInfo = item['goodsInfo'];
                          print('goodsInfo ==== $goodsInfo');
                          int refundStatus = item['refundStatus'];
                          print('refundStatus ==== $refundStatus');
                          return controller.type == 7
                              ? OrderItem(
                                  orderSn: item['id'] ?? '',
                                  orderTime: item['cd'] ?? '',
                                  totalAmount: item['priceOrder'] ?? 0.00,
                                  status: 6,
                                  goodsList: goodsInfo,
                                  id: item['id'],
                                  sn: item['paySn'],
                                  paySn: item['paySn'] ?? '',
                                  returnStatus: refundStatus,
                                  presaleData: item,
                                  isShowSpec: false,
                                  hasBonded: goodsInfo.any((item) => item['commodityType'] == 1),
                                )
                              : AfterSalesOrderItem(
                                  orderSn: item['paySn'] ?? '',
                                  orderTime: item['cd'] ?? '',
                                  totalAmount: item['priceOrder'] ?? 0.00,
                                  goodsList: item['goodsInfo'],
                                  id: item['id'],
                                  returnStatus: refundStatus,
                                );
                        })
                    : const NotFound(),
              )),
          Recommend()
        ],
      );
    });
  }

  /**
   *  订单状态
   */
  Widget _Tabs() => SizedBox(
        height: 30,
        child: TabBar(
            dividerColor: Colors.transparent,
            indicator: const BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 2, color: Colors.red),
              ),
            ),
            labelColor: Tools.fromHex("#F83F2D"),
            isScrollable: true,
            unselectedLabelColor: Colors.black,
            controller: controller.StatusTabController,
            tabAlignment: TabAlignment.center,
            tabs: const [
              Tab(
                text: "售后中",
              ),
              Tab(
                text: "售后记录",
              )
            ]),
      );
}
